package com.yycx.module.weixin.client.dto;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.WebUtils;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息发送请求对象
 *
 * @author zyf
 */
@Data
public class MessageRequest {

    /**
     * 公众号appId
     */
    private String appId;
    /**
     * 消息接收用户
     */
    private String openId;
    /**
     * 跳转地址
     */
    private String url;

    /**
     * 模板code
     */
    private String templateCode;

    /**
     * 消息发送来源
     */
    private String sourceType;
    /**
     * 处理消息内容
     */
    private String handlerName;

    /**
     * 其他参数
     */
    private Map<String, String> attr;

    /**
     * 使用类型(1.原生微信服务2.微信开放平台代公众号提供服务)
     */
    private Integer useWxTemplateType;


    public MessageRequest() {
        this(true);
    }


    public MessageRequest(Boolean isFromHttpServletRequest) {
        if (isFromHttpServletRequest) {
            HttpServletRequest request = WebUtils.getHttpServletRequest();
            Map<String, String> params = WebUtils.getParameterMap(request);
            this.appId = MapUtil.getStr(params, "appId");
            this.openId = MapUtil.getStr(params, "openId");
            this.url = MapUtil.getStr(params, "url");
            this.templateCode = MapUtil.getStr(params, "templateCode");
            this.handlerName = MapUtil.getStr(params, "handlerName");
            //删除map中的公共参数
            params.remove("appId");
            params.remove("openId");
            params.remove("url");
            params.remove("templateCode");
            params.remove("sign");
            this.attr = params;
        }
    }

    public void put(String key, String value) {
        if (FlymeUtils.isEmpty(attr)) {
            attr = new HashMap<>();
        }
        this.attr.put(key, value);
    }
}
